<?php
/**
 * Created by PhpStorm.
 * User: CCA
 * Date: 2019/9/28
 * Time: 17:15
 */

namespace app\common\model;


use think\Model;

class Question extends Model
{
    public $page_info;

    public function getQuestionList($condition,$page='',$order='id asc'){
        if ($page) {
            $result = $this->where($condition)->order($order)->paginate($page, false, ['query' => request()->param()]);
            $this->page_info = $result;
            return $result->items();
        } else {
            return db('question')->where($condition)->order($order)->limit(10)->select();
        }
    }

    public function addQuestion($data){
        $result = $this->insertGetId($data);
        return $result;
    }

    public function getOneQuestion($condition){
        $result = db('question')->where($condition)->find();
        return $result;
    }

    /**
     * 增加子类问题方法
     * @param $data
     * @param $parent_id
     *
     * @return bool
     */
    public static function addChildQuestion($data, $parent_id)
    {
        // 增加子类问题
        $model = new Question();
        $model->title = $data['title'];
        $model->gc_id1 = $data['gc_id1'];
        $model->gc_id2 = $data['gc_id2'];
        $model->gc_id3 = $data['gc_id3'];
        $model->parent_id = $parent_id;
        $model->created_at = date('Y-m-d H:i:s');
        $model->updated_at = date('Y-m-d H:i:s');
        if (!$model->save()) {
            return false;
        }
        // 处理父级问题中的下级问题id
        if (!self::_parentNextIdAdd($parent_id, $model->id)) {
            return false;
        }

        return true;
    }

    /**
     * 修改父类问题中的下级问题id字段
     * @param $parent_id
     * @param $model_id
     *
     * @return bool
     * @throws \think\exception\DbException
     */
    protected static function _parentNextIdAdd($parent_id, $model_id)
    {
        $model = self::get($parent_id);
        if (empty($model)) {
            return false;
        }
        if ($model->next_id == 0) {
            $model->next_id = '';
        }
        // 将问题信息中的下级id全部取出来
        $next_id = explode(',', $model->next_id);
        array_push($next_id, $model_id);
        $next_id = implode(',', $next_id);
        $model->next_id = trim($next_id, ',');
        $model->updated_at = date('Y-m-d H:i:s');
        if ($model->save()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 传入父级问题id返回所有父级问题对象
     * @param $parent_id
     */
    public static function getAllParentQuestions($parent_id)
    {
        if (is_string($parent_id)) {
            $parent_id = explode(',', $parent_id);
        }
        $parents = self::where('id', 'in', $parent_id)->select();

        return $parents;
    }

    public static function deleteParentId($id, $parent_id)
    {
        $model = self::find($id);
        if (empty($model)) return false;
        $parent_ids = explode(',', $model->parent_id);
        foreach ($parent_ids as $key => $val) {
            if ($val == $parent_id) {
                unset($parent_ids[$key]);
            }
        }
        $parent_ids = trim(implode(',', $parent_ids), ',');
        if (empty($parent_ids)) $parent_ids = 0;
        $model->parent_id = $parent_ids;
        $model->updated_at = date('Y-m-d H:i:s');
        if (!$model->save() || !self::deleteNextId($parent_id, $id)) {
            return false;
        }
        return true;
    }

    public static function deleteNextId($id, $next_id)
    {
        $model = self::find($id);
        if (empty($model)) return false;
        $next_ids = explode(',', $model->next_id);
        foreach ($next_ids as $key => $val) {
            if ($val == $next_id) {
                unset($next_ids[$key]);
            }
        }
        $next_ids = trim(implode(',', $next_ids), ',');
        if (empty($next_ids)) $next_ids = 0;
        $model->next_id = $next_ids;
        if (!$model->save()) {
            return false;
        }

        return true;
    }

    /**
     * 判断是否存在子类问题
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function hasNextQuestions($id)
    {
        $model = self::find($id);
        if ($model->next_id != 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 关联答案id
     * @param $id
     * @param $answer_id
     *
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function addAnswer($id, $answer_id)
    {
        $question = self::find($id);
        if (!$question) return false;
        if ($question->answer_id == 0) $question->answer_id = '';
        $answer_ids = explode(',', $question->answer_id);
        foreach ($answer_ids as $k=>$v) {
            if (empty($v)) unset($answer_ids[$k]);
        }
        array_push($answer_ids, $answer_id);
        $answer_ids = trim(implode(',', $answer_ids), ',');
        $question->answer_id = $answer_ids;
        if ($question->save()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取指定位置下的答案列表
     * @param $id
     *
     * @return bool|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getAnswer($id)
    {
        $question = self::find($id);
        if (!$question) return false;
        return Answer::getAnswerById($question->answer_id);
    }

    public static function getAnswerList($id)
    {
        $question = self::find($id);
        if (!$question) return false;
        return Answer::getAnswerByNotInId($question->answer_id);
    }

    public static function deleteAnswer($id, $answer_id)
    {
        $question = self::find($id);

        if (!$question) return false;

        $answer = explode(',', $question->answer_id);

        foreach ($answer as $key => $val) {
            if ($val == $answer_id) {
                unset($answer[$key]);
            }
        }

        $answer = trim(implode(',', $answer), ',');
        if (empty($answer)) $answer = 0;
        $question->answer_id = $answer;
        if ($question->save()) {
            return true;
        } else {
            return false;
        }

    }
    public static function getCategoryName($id) {
        $class = new Goodsclass();
        $cate = $class->find(['gc_id' => $id]);
        if ($cate) {
            return $cate->gc_name;
        }
        return '';


    }
}
